package insight.google.game.era.bo.policy.gamecard;

import insight.google.game.era.bo.GameActionLog;
import insight.google.game.era.bo.GameRoom;
import insight.google.game.era.bo.GameUser;
import insight.google.game.era.bo.Task;
import insight.google.game.era.bo.card.CharacterCard;
import insight.google.game.era.bo.policy.AbstractPolicy;
import insight.google.game.era.bo.policy.PolicyCenter;
import insight.google.game.era.client.proxy.PlayerClientProxy;
import insight.google.game.era.client.proxy.PlayerClientProxyImpl;
import insight.google.game.era.core.hub.HubServiceManager;
import insight.google.game.era.core.hub.UserEvents;
import insight.google.game.era.mgr.GameLogManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MilkPolicy extends AbstractPolicy {

    /**
	 * 
	 */
	private PlayerClientProxy playerClientProxy;
	private static final Log log = LogFactory.getLog(AttackPolicy.class);
	
    @Override
    public boolean execute(GameUser original, GameUser[] targets, Map params) {
        // TODO Auto-generated method stub
        playerClientProxy = PlayerClientProxyImpl.getInstance();
      //started monirul for game log
    	GameRoom currentRoom = (GameRoom)original.getCurrentRoom();
    	//GameLogManager gameLogManager = currentRoom.getGameLogManager();
    	//ended monirul for game log
        String jsComponentId = "";
        int lifeCount = 1;
        PolicyCenter pCenter = PolicyCenter.getInstance();
        for(int i=0; i<targets.length; i++){
            GameUser trgUser = targets[i];
            //get match data
            String cardName= (String)params.get("cardName");
            Map matchData = (Map)pCenter.getPolicyDataMap(cardName);
            String matchMethod = (String)matchData.get(PolicyCenter.KEY_METHOD);
            //get js component id
            jsComponentId = (String)params.get("jsComponenetId");
            log.debug("TAGRGET  USER  --- "+trgUser.getName());
//            playerClientProxy.milkCardPolicyExe(trgUser.getScriptProxy(), jsComponentId, matchMethod, lifeCount);

//            if(targets[0].getName().equals(original.getName())){
            	playerClientProxy.milkCardPolicyExe(original.getScriptProxy(), jsComponentId, matchMethod, lifeCount);
            	original.addBlood(1);
                //check user blood
                CharacterCard character = original.getCharacterCard();
                if(original.getBlood() >= character.getBlood()){
                	original.removeActiveCard("milk");
                }
//                playerClientProxy.setActiveCard(original.getScriptProxy(), jsComponentId, original.getActiveCards());
//                playerClientProxy.setActive(original.getScriptProxy(), jsComponentId, true);
                if(original.getBlood() > 1){
                    original.setActiveCards(original.getActiveCards(),jsComponentId);
                    original.setClientActive(true, jsComponentId);
                }
                else{
                	//remove task
        	        List<Task> tasklist = currentRoom.getTaskList();
        			 
        			int index=0;
        			for(Task task : tasklist){
        			    String matchValue = task.getMatchCardValue();
        			    String trgUserName = task.getTargetUserName();
        			    if(trgUserName.equals(original.getName())){
        			    	currentRoom.removeTask(index);
        			        break;
        			    }
        			    index++;
        			}
                }
                log.debug("ORG USER  --- "+original.getName());
//            }else{
//            	HashMap<String, String> data = new HashMap<String, String>();
//				data.put("lifeCount", String.valueOf( lifeCount));
//				data.put("methodName", matchMethod);
//            	HubServiceManager.getInstance().addPendingEvent(trgUser,
//						jsComponentId,
//						data, 
//						UserEvents.PLAYERPROXY_MILKCARD_POLICYEXECUTE.toString()
//						);
//            }
            
            //notify others 
//            currentRoom.notifyUserLife(trgUser, true);
            //gameLogManager.addGameLog(new GameActionLog(trgUser.getName() + " has added life "));
        }
        return true;
    }

}
